package com.example.server.web.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.server.web.domain.Question;
import com.example.server.web.exception.CustomException;
import com.example.server.web.mapper.QuestionMapper;
import com.example.server.web.service.QuestionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 作者
 * @since 2023-04-23
 */
@Service
public class QuestionServiceImpl extends ServiceImpl<QuestionMapper, Question> implements QuestionService {

    @Autowired
    private QuestionMapper questionMapper;
    @Override
    public List<Question> selectList(Long id) {
        LambdaQueryWrapper<Question> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Question::getQuestionnaireId,id);
        List<Question> questions = questionMapper.selectList(wrapper);
        return questions;
    }

    @Override
    //TODO 使用多线程解决性能问题
    public void deletesByQuestionnaireIds(List<Long> ids) {
        ids.forEach(id->{
            LambdaUpdateWrapper<Question> wrapper = new LambdaUpdateWrapper<>();
            wrapper.eq(Question::getQuestionnaireId,id);
            questionMapper.delete(wrapper);
        });
    }

    @Override
    public void delete(List<Long> ids) {
        questionMapper.deleteBatchIds(ids);
    }

    @Override
    public void modify(Question question) {
        int i = questionMapper.updateById(question);
        if(i!=1){
            throw new CustomException("更新失败!");
        }
    }

    @Override
    public void add(Question question) {
        int insert = questionMapper.insert(question);
        if(insert!=1){
            throw new CustomException("新增失败!");
        }
    }
}
